package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Outbound;
import com.woniu.supermarket.entity.OutboundCheck;
import com.woniu.supermarket.entity.OutboundDetail;
import com.woniu.supermarket.mapper.OutboundCheckMapper;
import com.woniu.supermarket.mapper.OutboundDetailMapper;
import com.woniu.supermarket.mapper.OutboundMapper;
import com.woniu.supermarket.service.IOutboundService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.OutboundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OutboundServiceImpl implements IOutboundService {


    @Autowired
    OutboundMapper outboundMapper;

    @Autowired
    OutboundDetailMapper outboundDetailMapper;

    @Autowired
    OutboundCheckMapper outboundCheckMapper;

    @Override
    public PageInfo<OutboundVo> findOutboundByCons(Integer now, Integer size, String outboundNumber, Integer status) {
        PageHelper.startPage(now,size);
        List<OutboundVo> lists = outboundMapper.selectOutboundByCons(outboundNumber, status);
        return new PageInfo<>(lists);
    }

    @Override
    @Transactional
    public int modifyOutbound(OutboundVo outboundVo) {
        //1.先将该订单下的订单详情删除
        outboundDetailMapper.deleteByOutboundId(outboundVo.getId());
        //2.修改订单信息
        outboundMapper.updateByPrimaryKey(outboundVo);

        //添加出库单详情
        List<OutboundDetail> outboundDetailList = outboundVo.getOutboundDetailList();
        for(OutboundDetail outboundDetail : outboundDetailList){
            outboundDetail.getcostPrice();
            outboundDetail.setOutboundDetailNumber(CodeCreateUtil.createOutboundDetailCode());
        }
        int i = outboundDetailMapper.insertBatchOutboundDetail(outboundDetailList);
        return i;
    }

    @Override
    @Transactional
    public int removeOutbound(int id) {
        int i = outboundMapper.deleteByPrimaryKey(id);
        outboundDetailMapper.deleteByOutboundId(id);
        return i;
    }

    @Override
    @Transactional
    public int addOutbound(OutboundVo outboundVo) {
        outboundMapper.insertOutbound(outboundVo);
        Integer id = outboundVo.getId();

        List<OutboundDetail> outboundDetailList = outboundVo.getOutboundDetailList();
        for(OutboundDetail outboundDetail : outboundDetailList){
            outboundDetail.getcostPrice();
            outboundDetail.setOutboundId(id);
            outboundDetail.setOutboundDetailNumber(CodeCreateUtil.createOutboundDetailCode());
        }
        int i = outboundDetailMapper.insertBatchOutboundDetail(outboundDetailList);
        return i;
    }

    @Override
    @Transactional
    public int addOutboundCheck(OutboundCheck outboundCheck) {
        //1.先修改出库订单状态
        Integer status = outboundCheck.getCheckResult();
        String code = outboundCheck.getOutboundNumber();
        outboundMapper.updateByCode(status,code);

        //2.将审核信息插入到审核信息表
        int i = outboundCheckMapper.insert(outboundCheck);
        return i;
    }
}
